<!--
 * @Author: Rain
 * @Date: 2021-11-22 11:29:41
 * @LastEditTime: 2023/12/03 11:45:48
 * @Description: Do Something
-->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div class="flex items-center flex-end search">
        <el-select
          v-model="query.atmrId"
          filterable
          placeholder="测试单元"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.atmrName"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-model="query.atmrType"
          filterable
          placeholder="测试类型"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.test_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <el-select
          v-model="query.groupId"
          filterable
          placeholder="团体类型"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in groupList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>

        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="wxId" label="微信Id" />
        <el-table-column prop="wxName" label="微信名" align="center" />
        <el-table-column prop="wxUser.mobile" label="手机号" align="center" />
        <el-table-column prop="atmrId" label="测试类型" align="center">
          <template slot-scope="{ row }">
            {{ row.atmrId | examFitler(typeList, dict.test_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="examTime" label="测试时间" align="center" />
        <el-table-column prop="examResult" label="测试结果" align="center" />
        <el-table-column
          v-if="checkPer(['admin', 'examResult:edit', 'examResult:del'])"
          label="操作"
          align="center"
          width="150px"
        >
          <template slot-scope="{ row }">
            <div class="flex items-center">
              <!-- <udOperation :data="row" :permission="permission" /> -->
              <el-button
                size="mini"
                type="success"
                style="margin-left: 5px"
                icon="el-icon-document"
                title="报告"
                @click="viewReport(row)"
              />
              <el-button
                size="mini"
                type="primary"
                style="margin-left: 5px !important"
                icon="el-icon-download"
                title="导出报告"
                @click="downloadReport(row)"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudExamResult from '@/api/record'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'

const defaultForm = {
  id: null,
  wxId: null,
  wxName: null,
  sex: null,
  atmrId: null,
  examTime: null,
  examResult: null,
  examContent: null,
  reportUrl: null,
  echart_pho: null
}
export default {
  name: 'Record',
  components: { pagination, crudOperation, rrOperation },
  filters: {
    optionFitler(item, list) {
      const { atmrName, atmrType } = item
      const type = list.find((i) => i.value === atmrType)
      return type && `${atmrName}-${type.label}`
    },
    examFitler(value, typeList, list) {
      const item = typeList.find((item) => item.id * 1 === value * 1)
      if (item) {
        const { atmrType, atmrName } = item
        const type = list.find((i) => i.value === atmrType)
        return type && `${atmrName}-${type.label}`
      }
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['test_type'],
  cruds() {
    return CRUD({
      title: '测试结果管理',
      url: 'api/examResult',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudExamResult }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'examResult:add'],
        edit: ['admin', 'examResult:edit'],
        del: ['admin', 'examResult:del']
      },
      rules: {},
      queryTypeOptions: [{ key: 'atmrId', display_name: '测试类型' }],
      typeList: [],
      groupList: []
    }
  },
  mounted() {
    this.queryTestType()
    this.queryGroupList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 查询测试类型
    queryTestType() {
      this.$http.get(`/api/atmr`).then((_) => {
        const { content } = _
        this.typeList = content
      })
    },
    // 查询团体类型
    queryGroupList() {
      this.$http.get(`/api/groupTest`).then((_) => {
        const { content } = _
        this.groupList = content
        console.log(content)
      })
    },
    viewReport(row) {
      const { reportUrl } = row
      window.open(reportUrl)
    },
    downloadReport(row) {
      const { reportUrl } = row
      this.$http
        .post(
          `/api/examResult/export`,
          { pathList: [reportUrl] },
          { responseType: 'blob' }
        )
        .then((_) => {
          const blob = new Blob([_], { type: 'application/zip' })
          const url = window.URL.createObjectURL(blob)
          const a = document.createElement('a')
          a.href = url
          a.click()
          window.URL.revokeObjectURL(url)
        })
    },
    downloadReports() {
      if (this.crud.selections.length > 0) {
        const urls = this.crud.selections.map((item) => item.echart_pho)
        this.$http
          .post(`/api/examResult/export`, { pathList: urls }, { responseType: 'blob' })
          .then((_) => {
            const blob = new Blob([_], { type: 'application/zip' })
            const url = window.URL.createObjectURL(blob)
            const a = document.createElement('a')
            a.href = url
            a.click()
            window.URL.revokeObjectURL(url)
          })
      }
    }
  }
}
</script>

<style scoped></style>
